<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'user') {
    header("Location: ../login.php");
    exit();
}

require '../config.php';

$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];

// 获取用户的帖子
$posts_per_page = 5;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $posts_per_page;

$stmt = $conn->prepare("SELECT id, title, created_at FROM posts WHERE user_id = ? LIMIT ?, ?");
$stmt->bind_param("iii", $user_id, $offset, $posts_per_page);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($post_id, $post_title, $post_created_at);

// 获取总帖子数
$total_posts_stmt = $conn->prepare("SELECT COUNT(*) FROM posts WHERE user_id = ?");
$total_posts_stmt->bind_param("i", $user_id);
$total_posts_stmt->execute();
$total_posts_stmt->bind_result($total_posts);
$total_posts_stmt->fetch();
$total_pages = ceil($total_posts / $posts_per_page);

$total_posts_stmt->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户主页 - 论坛</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        body {
            background-image: url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0');
            background-size: cover;
            background-position: center;
            color: #4c4948;
            font-family: 'Lato', Arial, sans-serif;
            margin: 0;
            display: flex;
            min-height: 100vh;
        }
        .overlay {
            background: rgba(255, 255, 255, 0.8);
            width: 100%;
            display: flex;
        }
        .sidebar {
            background-color: #2d3748;
            color: white;
            width: 250px;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            border-radius: 15px;
            margin: 20px;
        }
        .sidebar a {
            color: white;
            display: block;
            padding: 10px 20px;
            margin: 10px 0;
            background: #3b4252;
            border-radius: 25px;
            transition: background 0.3s;
        }
        .sidebar a:hover {
            background: #a0aec0;
        }
        .main-content {
            flex: 1;
            padding: 20px;
            display: flex;
            flex-direction: column;
            margin: 20px;
            border-radius: 15px;
            background: #fff;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .bg-header {
            background-image: url('https://images.unsplash.com/photo-1542281286-9e0a16bb7366');
            background-size: cover;
            background-position: center;
            height: 200px;
            border-radius: 15px;
            position: relative;
            overflow: hidden;
        }
        .bg-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 0;
        }
        .bg-header h2 {
            position: relative;
            z-index: 1;
            color: white;
            text-align: center;
            line-height: 200px;
            font-size: 2rem;
            margin: 0;
        }
        .card {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            margin-top: 20px;
        }
        .post-title a {
            color: #1a73e8;
            transition: color 0.3s;
        }
        .post-title a:hover {
            color: #0c4ea2;
        }
        .pagination a {
            display: inline-block;
            margin: 0 5px;
            padding: 10px 15px;
            background: #e2e8f0;
            border-radius: 10px;
            transition: background 0.3s;
        }
        .pagination a:hover {
            background: #cbd5e0;
        }
        .pagination .current-page {
            background: #1a73e8;
            color: white;
        }
    </style>
</head>
<body>
    <div class="overlay">
        <div class="sidebar">
            <h2 class="text-3xl font-bold mb-8">用户主页</h2>
            <nav class="w-full">
                <a href="../feed.php" class="text-lg">帖子</a>
                <a href="create_post.php" class="text-lg">发布新帖子</a>
                <a href="notifications.php" class="text-lg">通知</a>
                <a href="../logout.php" class="text-lg text-red-500 hover:text-red-700">退出登录</a>
            </nav>
        </div>
        <div class="main-content">
            <div class="bg-header">
                <h2>欢迎, <?php echo htmlspecialchars($username); ?></h2>
            </div>
            <div class="card">
                <h3 class="text-2xl font-bold text-gray-800 mb-8">我的帖子</h3>
                <ul class="post-list space-y-8">
                    <?php
                    if ($stmt->num_rows > 0) {
                        while ($stmt->fetch()) {
                            echo "<li class='border-b pb-8'>
                                <div class='post-title text-xl font-semibold mb-2'><a href='view_post.php?id=$post_id'>" . htmlspecialchars($post_title) . "</a></div>
                                <div class='post-meta text-sm text-gray-500 mb-4'>发布时间: $post_created_at</div>
                                <div class='post-actions space-x-4'>
                                    <a href='edit_post.php?id=$post_id' class='bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-700'>编辑</a>
                                    <a href='delete_post.php?id=$post_id' class='bg-red-500 text-white px-4 py-2 rounded-md hover:bg-red-700' onclick='return confirm(\"确定要删除这篇帖子吗？\");'>删除</a>
                                </div>
                            </li>";
                        }
                    } else {
                        echo "<li class='text-gray-500'>暂无帖子</li>";
                    }
                    $stmt->close();
                    $conn->close();
                    ?>
                </ul>
                <div class="pagination mt-8 flex justify-center space-x-2">
                    <?php
                    if ($total_pages > 1) {
                        for ($i = 1; $i <= $total_pages; $i++) {
                            if ($i == $page) {
                                echo "<span class='current-page px-4 py-2'>$i</span>";
                            } else {
                                echo "<a href='index.php?page=$i' class='px-4 py-2'>$i</a>";
                            }
                        }
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
